#include "cppTutorMultiThreading.h"
#include "cppTutorMultiThreadingFunctions.h"

#define NUM_EMPLOYEES 2 /* size of each array. */

// struct employee 
// {
    // int number;
    // int id;
    // char first_name[20];
    // char last_name[30];
    // char department[30];
    // int room_number;
// };

// /* global variable - our employees array, with 2 employees */
// struct employee employees[] = 
// {
    // { 1, 00001, "Shalini", "Gupta", "MD", 101},
    // { 2, 00002, "Saurabh", "Gupta", "CEO", 202}
// };

// /* global variable - employee of the day. */
// struct employee employee_of_the_day;

// int i;
// int thr_id1; /* thread ID for the first new thread */
// int thr_id2; /* thread ID for the second new thread */
// pthread_t p_thread1; /* first thread's structure */
// pthread_t p_thread2; /* second thread's structure */
// int num1; /* thread 1 employee number */
// int num2; /* thread 2 employee number */
// struct employee eotd; /* local copy of 'employee of the day'. */
// struct employee* worker; /* pointer to currently checked employee */ 

// /* function to copy one employee struct into another */
// void copy_employee(struct employee* from, struct employee* to);

// /* function to be executed by the variable setting threads thread */
// void* mutex_example_do_loop(void* data);

// /* global mutex for our program. assignment initializes it */
// pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;

/* function to be executed by the new thread when more than one thread is used*/
void *PrintHello(void *threadarg)
{
    Log << "ENTER : PrintHello ()" << endl;
	struct thread_data *my_data;
	my_data = (struct thread_data *) threadarg;

	cout << "Thread ID : " << my_data->thread_id ;
	cout << " Message : " << my_data->message << endl;
    Log << "EXIT  : PrintHello ()" << endl;
#ifdef WIN32
// windows thread specific
#else	
    pthread_exit(NULL);
#endif
	return NULL;
}

void *do_loop(void* data)
{
    Log << "ENTER : do_loop ()" << endl;
    int i; /* counter, to print numbers */
    int j; /* counter, for delay */
    int me = *((int*)data); /* thread identifying number */
    for (i=0; i<10; i++) 
    {
        for (j=0; j<500000; j++) /* delay loop */
        ;
        printf("'%d' - Got '%d'\n", me, i);
    }
    Log << "EXIT  : do_loop ()" << endl;
    /* terminate the thread */
#ifdef WIN32
    // windows thread specific
#else
	pthread_exit(NULL);
#endif
    return NULL;
}

void *wait(void *t)
{
	//int i;
	long tid;

	tid = (long)t;
	SLEEP (10);
	cout << "Sleeping in thread " << endl;
	cout << "Thread with id : " << tid << "  ...exiting " << endl;
#ifdef WIN32
	// windows thread specific
#else
	pthread_exit(NULL);
#endif
	return NULL;
}